import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import utils.*;

import db.DbConnect;

public class AdminServer {
	
	private Logger log = new Logger();
	private int nr_loc_ocup;
	
	private int computePrice(int duration){
		
		return duration*30;
	}
	public String adaugare_Zbor (String source, String dest, int departureDay, int departureHour, int duration, int numberOfSeats, String flightID){
		//departureDay a anului (intre 1 si 365)
		// ora departureHour (toate zborurile decoleaza la ora fixa, intre 0 si 23)
		if(departureDay<1||departureDay>365)
			return "Verify departure day (must be int 1-365)";
		if(departureHour<0||departureHour>23)
			return "Verify departure hour (must be int 0-23)";
		ResultSet rs;
		String query = new String();
		
		try {
			query  = "INSERT INTO `Curse` (`ID`, `Start`, `Stop`, `Ora_plecare`, `Zi_plecare`, `Durata`, `Nr_locuri_disp`, `Locuri_ocupate`, `Pret`)" +
					" VALUES ('"+
					flightID+"', '"+
					source+"', '"+
					dest+"', '"+
					departureHour+"', '"+
					departureDay+"', '"+
					duration+"', '"+
					departureHour+"', '"+
					0+"', '"+
					computePrice(duration)+"');";
			
			log.addToLog(query);
			DbConnect.IUDQuery(query); 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "Succesfully added new flight!";
	
	}

	public void listFlights(String padding){
		ResultSet rs;
		String query = new String();
		
		try {
			query  = "Select ID, Start,Stop,Zi_plecare,Ora_plecare from Curse ;";
			log.addToLog(query);
			
			rs = DbConnect.query(query);
			while(rs.next()){
				int flightID = rs.getInt(1);
				String start = rs.getString(2);
				String stop = rs.getString(3);
				int zipl = rs.getInt(4);
				int opl = rs.getInt(5);
				System.out.println(padding+"["+flightID+";"+start+"->"+stop+";"+zipl+"-"+opl+":00]");
			}
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public boolean checkFlighID(String flightID){
		
		ResultSet rs;
		String query = new String();
		if(flightID.equals("")){
			
			log.addToLog("Invalid flight ID");
			return false;
		}
		
		
		try {
			query  = "Select Locuri_ocupate from Curse where ID='"+flightID+"';";
			log.addToLog(query);
			
			rs = DbConnect.query(query);
			if(rs.next()){
				int nr_loc_ocup = rs.getInt(1);
				
				log.addToLog("Found requested flight:"+flightID);
				//log.addToLog("Schedueling:"+nr_loc_ocup+"to delete from booked");
				return true;
			}
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;

	}
	/**
	 * anulare Zbor va sterge informatia referitoare la zborul identificat prin flightID (zborul fiind anulat).
	 * @param flightID
	 */
	public String anulare_Zbor (String flightID){
		ResultSet rs;
		String query = new String();
		if(flightID.equals(""))
			return "";
		
		
		
		try {
				
				query  = "DELETE from Curse where ID='"+flightID+"';";
				DbConnect.IUDQuery(query); 
				
				
				query  = "SELECT ID,Nr_bilete FROM Booked WHERE ID_Zboruri LIKE '%"+flightID+";%';";
				log.addToLog(query);
				rs = DbConnect.query(query);
				
				
				ArrayList<String>id_booking_del = new ArrayList<String>();
				
				while(rs.next()){
					String id_booking = rs.getString(1);
					log.addToLog(id_booking);
					id_booking_del.add(id_booking);
					int nr_bilete = rs.getInt(2);
					nr_loc_ocup -= nr_bilete;
					
				}
				for(int i = 0; i < id_booking_del.size(); i++){
					query  = "DELETE from Booked where ID='"+id_booking_del.get(i)+"' ;";
					DbConnect.IUDQuery(query); 
				}
				if(nr_loc_ocup==0&&id_booking_del.size()>0)
					log.addToLog("Succesfully canceled all bookings");
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "Succesfully deleted flight!";

		
	}



	
}
